package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.WeiboMapper;
import cn.tedu._04weibo.pojo.dto.WeiboAddParam;
import cn.tedu._04weibo.pojo.entity.Weibo;
import cn.tedu._04weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@Validated
@RestController
@RequestMapping("/v1/weibo")
@Tag(name = "02-资讯管理模块")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("add")
    @Operation(summary = "添加微博")
    @ApiOperationSupport(order = 100)
    public JsonResult add(@RequestBody WeiboAddParam weiboAddParam, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setUserId(userVO.getId());
        weibo.setCreateTime(new Date());
        log.debug("添加微博：{}",weibo);
        int rows =weiboMapper.insertWeibo(weibo);
        if (rows!=1){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok();
    }

    @GetMapping("selectIndex")
    @Operation(summary = "微博首页列表")
    @ApiOperationSupport(order = 200)
    public JsonResult list(){
        return JsonResult.ok(weiboMapper.selectWeiboList());
    }

    @GetMapping("selectById")
    @Operation(summary = "微博详情")
    @ApiOperationSupport(order = 300)
    public JsonResult selectById(@Range(min = 200L,max = 500l,message = "id范围在200-500之间") Long id){
        return JsonResult.ok(weiboMapper.selectWeiboById(id));
    }
}
